package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.beans.CommonResonseBean;
import com.tongyi.lizhishop.common.BaseActivity;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.managers.APIServiceManager;
import com.tongyi.lizhishop.utils.PromptDialogUtils;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.leefeng.promptlibrary.PromptDialog;

public class FeedBackActivity extends BaseActivity {

    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.content)
    EditText content;
    @BindView(R.id.submitBtn)
    Button submitBtn;
    @BindView(R.id.activity_feed_back)
    LinearLayout activityFeedBack;
    private PromptDialog prompDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        ButterKnife.bind(this);
        initTitleBarView(titlebar, "意见反馈");
        prompDialog = PromptDialogUtils.getPrompDialog(this);
    }

    @OnClick(R.id.submitBtn)
    public void onViewClicked() {

        String content = this.content.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort("请输入反馈的内容");
            return;
        }

        prompDialog.showLoading("请等待", false);
        APIServiceManager.getInstance().create(MessageApi.class)
                .addOpinion(SPUtils.getInstance().getString("userid"), content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<CommonResonseBean>() {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        if ("succ".equals(value.getRe())) {
                            prompDialog.showSuccess("成功", false);
                            finish();
                        } else {
                            prompDialog.showError("提交失败", false);
                            ToastUtils.showLong(value.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                        prompDialog.showError("提交失败", false);
                        ToastUtils.showShort("无法连接网络");
                    }
                });
    }
}
